package com.itheima.ZuoYe13;

import java.math.BigDecimal;
import java.math.RoundingMode;

/*
有以下double数组：
		double[] arr = {0.1,0.2,2.1,3.2,5.56,7.21};
	请编程计算它们的总值及平均值(四舍五入保留小数点后2位)
 */
public class BigDecimalDemo {
    public static void main(String[] args) {
        double[] arr = {0.1,0.2,2.1,3.2,5.56,7.21};
        BigDecimal sum = new BigDecimal(0);
        for (int i = 0; i < arr.length; i++) {
            BigDecimal a = BigDecimal.valueOf(arr[i]);
            sum = sum.add(a);
        }
        BigDecimal c = new BigDecimal(arr.length);
        BigDecimal avg = sum.divide(c, 2, RoundingMode.HALF_UP);
        System.out.println("总数："+sum);
        System.out.println("平均值："+avg);
    }

}
